﻿using DTcms.Common;
using System.Collections.Generic;
using System.Data;
using Model = DTcms.Model;
using BLL = DTcms.BLL;
using System.Text.RegularExpressions;
using System.Text;
using System.Linq;
using DTcms.Web.Mvc.UI;

namespace System.Web.Mvc.Html
{
    public static class HtmlHelperArticle
    {
        public static BaseController _base = new BaseController();

        #region linkurl
        /// <summary>
        /// 返回URL重写统一链接地址
        /// </summary>
        public static string linkurl(this HtmlHelper htmlHelper, string _key, params object[] _params)
        {
            return _base.linkurl(_key, _params);
        }

        public static string linkallurl(this HtmlHelper htmlHelper, string _key, params object[] _params)
        {
            return HttpContext.Current.Request.Url.Authority.ToLower() + linkurl(htmlHelper, _key, _params);
        }

        /// <summary>
        /// 返回分页字符串
        /// </summary>
        /// <param name="pagesize">页面大小</param>
        /// <param name="pageindex">当前页</param>
        /// <param name="totalcount">记录总数</param>
        /// <param name="_key">URL映射Name名称</param>
        /// <param name="_params">传输参数</param>
        public static string get_page_link(this HtmlHelper htmlHelper, int pagesize, int pageindex, int totalcount, string _key, params object[] _params)
        {
            return _base.get_page_link(pagesize, pageindex, totalcount, _key, _params);
        }

        /// <summary>
        /// 返回分页字符串
        /// </summary>
        /// <param name="pagesize">页面大小</param>
        /// <param name="pageindex">当前页</param>
        /// <param name="totalcount">记录总数</param>
        /// <param name="linkurl">链接地址</param>
        public static string get_page_link(this HtmlHelper htmlHelper, int pagesize, int pageindex, int totalcount, string linkurl)
        {
            return _base.get_page_link(pagesize, pageindex, totalcount, linkurl);
        }

        /// <summary>
        /// 返回URL重写统一链接地址
        /// </summary>
        /// <param name="_key">调用名称</param>
        /// <param name="_call_index">调用名称</param>
        /// <param name="_id">文章ID</param>
        /// <returns></returns>
        public static string queryurl(this HtmlHelper htmlHelper, string _key, string call_index, object id)
        {
            return _base.queryurl(_key, call_index, id);
        }

        /// <summary>
        /// 返回URL重写统一链接地址
        /// </summary>
        /// <param name="_key">调用名称</param>
        /// <param name="_url">链接地址</param>
        /// <param name="_call_index">调用名称</param>
        /// <param name="_id">文章ID</param>
        /// <returns></returns>
        public static string queryurl(this HtmlHelper htmlHelper, string _key, string url, string call_index, object id)
        {
            return _base.queryurl(_key, url, call_index, id);
        }

        /// <summary>
        /// 合并扩展参数
        /// </summary>
        /// <param name="url">URL地址</param>
        /// <param name="param">参数</param>
        /// <returns></returns>
        public static string mergeurl(this HtmlHelper htmlHelper, string url, string param)
        {
            return _base.mergeurl(url, param);
        }

        /// <summary>
        /// 查找匹配的URL
        /// </summary>
        /// <param name="channel_id">频道ID</param>
        /// <param name="call_index">调用名</param>
        /// <param name="id">ID</param>
        /// <returns></returns>
        public static string get_url_rewrite(this HtmlHelper htmlHelper, int channel_id, string call_index, int id)
        {
            return _base.get_url_rewrite(channel_id, call_index, id);
        }

        /// <summary>
        /// 查找匹配的URL
        /// </summary>
        /// <param name="channel_id">频道ID</param>
        /// <param name="call_index">调用名</param>
        /// <param name="id">ID</param>
        /// <returns></returns>
        public static string get_url_rewrite(this HtmlHelper htmlHelper, string channel_name, string type, string call_index, int id)
        {
            return _base.get_url_rewrite(channel_name, type, call_index, id);
        }
        #endregion

        #region 获取上一条下一条的链接
        /// <summary>
        /// 获取上一条下一条的链接
        /// </summary>
        /// <param name="urlkey">urlkey</param>
        /// <param name="type">-1代表上一条，1代表下一条</param>
        /// <param name="defaultvalue">默认文本</param>
        /// <param name="callIndex">是否使用别名，0使用ID，1使用别名</param>
        /// <param name="category_id">类别ID</param>
        /// <param name="channel_id">频道ID</param>
        /// <param name="channel">频道名</param>
        /// <param name="id">文章ID</param>
        /// <returns>A链接</returns>
        public static string get_prevandnext_article(this HtmlHelper htmlHelper, string urlkey, int type, string defaultvalue, int callIndex, int category_id, string channel, int id)
        {
            return _base.get_prevandnext_article(urlkey, type, defaultvalue, callIndex, category_id, channel, id);
        }
        #endregion

        #region 文章列表
        /// <summary>
        /// 文章列表
        /// </summary>
        /// <param name="channel_name">频道名称</param>
        /// <param name="top">显示条数</param>
        /// <param name="strwhere">查询条件</param>
        /// <returns>DataTable</returns>
        public static DataTable get_article_list(this HtmlHelper htmlHelper, string channel_name, int top, string strwhere)
        {
            return _base.get_article_list(channel_name, top, strwhere);
        }

        /// <summary>
        /// 文章列表
        /// </summary>
        /// <param name="channel_name">频道名称</param>
        /// <param name="category_id">分类ID</param>
        /// <param name="top">显示条数</param>
        /// <param name="strwhere">查询条件</param>
        /// <returns>DataTable</returns>
        public static DataTable get_article_list(this HtmlHelper htmlHelper, string channel_name, int category_id, int top, string strwhere)
        {
            return _base.get_article_list(channel_name, category_id, top, strwhere);
        }

        /// <summary>
        /// 文章列表
        /// </summary>
        /// <param name="channel_name">频道名称</param>
        /// <param name="category_id">分类ID</param>
        /// <param name="top">显示条数</param>
        /// <param name="strwhere">查询条件</param>
        /// <param name="orderby">排序</param>
        /// <returns>DataTable</returns>
        public static DataTable get_article_list(this HtmlHelper htmlHelper, string channel_name, int category_id, int top, string strwhere, string orderby)
        {
            return _base.get_article_list(channel_name, category_id, top, strwhere, orderby);
        }

        /// <summary>
        /// 文章分页列表(自定义页面大小)
        /// </summary>
        /// <param name="channel_name">频道名称</param>
        /// <param name="category_id">分类ID</param>
        /// <param name="page_size">页面大小</param>
        /// <param name="page_index">当前页码</param>
        /// <param name="strwhere">查询条件</param>
        /// <param name="orderby">排序</param>
        /// <param name="totalcount">总记录数</param>
        /// <returns>DateTable</returns>
        public static DataTable get_article_list(this HtmlHelper htmlHelper, string channel_name, int category_id, int page_size, int page_index, string strwhere, string orderby, out int totalcount)
        {
            return _base.get_article_list(channel_name, category_id, page_size, page_index, strwhere, orderby, out totalcount);
        }

        /// <summary>
        /// 文章分页列表(自动页面大小)
        /// </summary>
        /// <param name="channel_name">频道名称</param>
        /// <param name="category_id">分类ID</param>
        /// <param name="page_size">分页大小</param>
        /// <param name="page_index">当前页码</param>
        /// <param name="strwhere">查询条件</param>
        /// <param name="totalcount">总记录数</param>
        /// <param name="_key">URL配置名称</param>
        /// <param name="_params">传输参数</param>
        /// <returns>DataTable</returns>
        public static DataTable get_article_list(this HtmlHelper htmlHelper, string channel_name, int category_id, int page_size, int page_index, string strwhere, out int totalcount, out string pagelist, string _key, params object[] _params)
        {
            return _base.get_article_list(channel_name, category_id, page_size, page_index, strwhere, out totalcount, out pagelist, _key, _params);
        }

        /// <summary>
        /// 文章分页列表(自动页面大小)
        /// </summary>
        /// <param name="channel_name">频道名称</param>
        /// <param name="category_id">分类ID</param>
        /// <param name="page_size">分页大小</param>
        /// <param name="page_index">当前页码</param>
        /// <param name="strwhere">查询条件</param>
        /// <param name="totalcount">总记录数</param>
        /// <param name="_key">URL配置名称</param>
        /// <param name="_call_index">调用名</param>
        /// <param name="_id">类别ID</param>
        /// <returns>DataTable</returns>
        public static DataTable get_article_list_pag(this HtmlHelper htmlHelper, string channel_name, int category_id, int page_size, int page_index, string strwhere, out int totalcount, out string pagelist, string _key, string _call_index, int _id)
        {
            if (!string.IsNullOrEmpty(_call_index))
            {
                return get_article_list(htmlHelper, channel_name, category_id, page_size, page_index, strwhere, out totalcount, out pagelist, _key, _call_index, "__id__");
            }
            else
            {
                return get_article_list(htmlHelper, channel_name, category_id, page_size, page_index, strwhere, out totalcount, out pagelist, _key, _id, "__id__");
            }
        }

        /// <summary>
        /// 文章分页列表(可排序)
        /// </summary>
        /// <param name="channel_name">频道名称</param>
        /// <param name="category_id">分类ID</param>
        /// <param name="page_size">分页大小</param>
        /// <param name="page_index">当前页码</param>
        /// <param name="strwhere">查询条件</param>
        /// <param name="orderby">排序</param>
        /// <param name="totalcount">总记录数</param>
        /// <param name="_key">URL配置名称</param>
        /// <param name="_params">传输参数</param>
        /// <returns>DataTable</returns>
        public static DataTable get_article_list(this HtmlHelper htmlHelper, string channel_name, int category_id, int page_size, int page_index, string strwhere, string orderby, out int totalcount, out string pagelist, string _key, params object[] _params)
        {
            return _base.get_article_list(channel_name, category_id, page_size, page_index, strwhere, orderby, out totalcount, out pagelist, _key, _params);
        }
        #endregion

        #region 文章附属内容
        //图片列表
        public static List<Model.article_albums> get_article_albums(this HtmlHelper htmlHelper, int channel_id, int article_id)
        {
            return _base.get_article_albums(channel_id, article_id);
        }

        //附件列表
        public static List<Model.article_attach> get_article_attach(this HtmlHelper htmlHelper, int channel_id, int article_id)
        {
            return _base.get_article_attach(channel_id, article_id);
        }

        //附件详情
        public static Model.article_attach get_article_attach_info(this HtmlHelper htmlHelper, int attach_id)
        {
            return _base.get_article_attach_info(attach_id);
        }

        /// <summary>
        /// 自定义参数列表
        /// </summary>
        /// <param name="channel_id"></param>
        /// <param name="article_id"></param>
        /// <returns></returns>
        public static List<Model.article_attribute> get_article_attribute(this HtmlHelper htmlHelper, int channel_id, int article_id)
        {
            return _base.get_article_attribute(channel_id, article_id);
        }

        /// <summary>
        /// 将日期转为指定格式
        /// </summary>
        /// <param name="time"></param>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string datetostr(this HtmlHelper htmlHelper, object time, string str)
        {
            return _base.datetostr(time, str);
        }

        /// <summary>
        /// 截取字符串长度标签
        /// </summary>
        /// <param name="str"></param>
        /// <param name="num"></param>
        /// <returns></returns>
        public static string cutstring(this HtmlHelper htmlHelper, object str, int num)
        {
            return _base.cutstring(str, num);
        }

        //文章相册图片数量
        public static int get_article_albums_count(this HtmlHelper htmlHelper, int channel_id, int article_id)
        {
            return _base.AlbumsCount(channel_id, article_id);
        }

        //标题
        public static string get_article_title(this HtmlHelper htmlHelper, int articleid, int channleid)
        {
            return _base.get_article_title(channleid, articleid);
        }

        //下载数量
        public static int get_article_attach_count(this HtmlHelper htmlHelper, int attach_id)
        {
            return _base.AttachCount(attach_id);
        }

        //下载数量
        public static int get_article_attach_count(this HtmlHelper htmlHelper, int articleid, int channle)
        {
            return _base.AttachCount(articleid, channle);
        }

        /// <summary>
        /// 附件下载列表
        /// </summary>
        /// <param name="page_size">页面大小</param>
        /// <param name="page_index">当前页码</param>
        /// <param name="strwhere">查询条件</param>
        /// <param name="totalcount">总记录数</param>
        /// <returns>DateTable</returns>
        public static List<Model.user_attach_log> get_article_attach_log(this HtmlHelper htmlHelper, int page_size, int page_index, string strwhere, out int totalcount)
        {
            return _base.get_article_attach_log(page_size, page_index, strwhere, out totalcount);
        }

        /// <summary>
        /// 附件下载列表
        /// </summary>
        /// <param name="top">显示条数</param>
        /// <param name="strwhere">查询条件</param>
        /// <returns></returns>
        public static List<Model.user_attach_log> get_article_attach_log(this HtmlHelper htmlHelper, int top, string strwhere)
        {
            return _base.get_article_attach_log(top, strwhere);
        }

        /// <summary>
        /// 检查用户是否下载过该附件
        /// </summary>
        public static bool get_article_attach_exists_log(this HtmlHelper htmlHelper, int attach_id, int user_id)
        {
            return _base.ExistsLog(attach_id, user_id);
        }
        #endregion

        #region 其他
        //判断是否为null，并返回当前Model扩展字段值
        public static string ArticleField(this HtmlHelper htmlHelper, Dictionary<string, string> m, string field)
        {
            return _base.ArticleField(m, field);
        }

        /// <summary>
        /// 获取对应的频道标题
        /// </summary>
        /// <param name="channel_id">频道ID</param>
        /// <returns>String</returns>
        public static string get_channel_title(this HtmlHelper htmlHelper, int channel_id)
        {
            return _base.get_channel_title(channel_id);
        }

        //计算两个日期之间相差的天数
        public static int DateDiff(this HtmlHelper htmlHelper, DateTime dateStart, DateTime dateEnd)
        {
            return _base.DateDiff(dateStart, dateEnd);
        }

        //统计数量
        public static int ArticleCount(this HtmlHelper htmlHelper)
        {
            return _base.ArticleCount();
        }

        //判断checkbox是否是同一个值 并返回checked
        public static string ArticleCheckbox(this HtmlHelper htmlHelper, object filed, object value)
        {
            return _base.ArticleCheckbox(filed, value);
        }
        #endregion
    }
}